// 注释
let app = new Vue({
	el: "#app",
	data() {
		return {
			leftbar_show: true,
			tableHeight: window.innerHeight - 90,

		    positionList : [],
		    tableData : [],
		    queryForm : {
		        position : {
		            'id' : '',
		            'name' : ''
		        }
		    },
		}
	},
	methods: {
		// 显示侧边栏
		showLeftBar() {
			if (this.leftbar_show) {
				this.leftbar_show = false
			} else {
				this.leftbar_show = true
			}
		},

		// 主查询
		async query() {
			// 显示加载框
			const loading = this.$loading(LOAD_STYLE);
			try {
				let res = await axios.post('api/authoritymanagement/query', this.queryForm);
				if (res.code === 200) {
				    if (this.queryForm.position.id === ''){
				        this.positionList = res.data.data_position;
				    }
					this.tableData = res.data.data;

					this.$message.success(res.message);
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('查询请求出错！', e.message);
			}
			// 关闭加载框
			loading.close();
		},

        //修改
        async update(row) {
            this.tableData[row.id].jurisdiction = ! this.tableData[row.id].jurisdiction;
            // 显示加载框
			const loading = this.$loading(LOAD_STYLE);

			try {
				let res = await axios.post('api/authoritymanagement/update', this.tableData[row.id]);
				if (res.code === 200) {
				   this.query();
				} else {
					this.$message.error(res.message);
				}
			} catch (e) {
				this.$message.error('修改出错！', e.message);
			}

			// 关闭加载框
			loading.close();
        },

        //左侧选择职位
		handleNodeClick(data) {
		    this.queryForm.position = data;
		    this.query();
        },

        // 增加职位弹窗
        async insert() {
            this.$prompt('请输入添加职位', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
            }).then(async ({ value }) => {
              this.$message({
                type: 'success',
                message: '职位是: ' + value
              });

                try {
                    let res = await axios.post('api/authoritymanagement/insert', {'value': value});
                    if (res.code === 200) {
                        this.positionList = res.data.data_position;
                        this.$message.success(res.message);
                    } else {
                        this.$message.error(res.message);
                    }
                } catch (e) {
                    this.$message.error('添加出错！', e.message);
                };

            }).catch(() => {
              this.$message({
                type: 'info',
                message: '取消输入'
              });
            });
        },

        // 删除职位弹窗
        async deletet() {
            console.log(this.queryForm.position);

            this.$confirm('此操作将删除该职位, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                try {
                    let res = await axios.post('api/authoritymanagement/deletet', this.queryForm.position);
                    if (res.code === 200) {
                        this.positionList = res.data.data_position;

                        this.$message.success(res.message);
                    } else {
                        this.$message.error(res.message);
                    }
                } catch (e) {
                    this.$message.error('删除出错！', e.message);
                };
                this.tableData = [];
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },

	},

	async mounted() {

		// 自动调整表格高度
		window.onresize = () => {
			return (() => {
				this.tableHeight = window.innerHeight - 90;
			})()
		};

		//页面初始化
		this.query();


	},
});







